package action;

import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.util.Comparator;
import java.util.List;

import comparator.BirthComparator;
import comparator.IDComparator;
import pojo.Customer;
import processor.Processor;
import processor.impl.ProcessorImpl;
import scanner.DataScanner;
import scanner.impl.DataScannerImpl;

public class Action {
	private Comparator<Customer> comparator;
	private DataScanner dataScanner;
	private Processor processor;
	private String format;
	
	public StringBuilder action(String operate, String dataKind, String sortedBy) throws Exception{
		
		format = dataKind;
		if(sortedBy.equals("id")){
			comparator = new IDComparator();
		}else{
			comparator = new BirthComparator();
		}
		
		if(operate.equals("L")){
			dataScanner = new DataScannerImpl(new FileInputStream("data.txt"));
			List<Customer> list = dataScanner.scan();
			processor = new ProcessorImpl(list, format);
			StringBuilder string = processor.process(comparator);
			return string;
		}else{
			dataScanner = new DataScannerImpl(new FileInputStream("data.txt"));
			List<Customer> list = dataScanner.scan();
			processor = new ProcessorImpl(list, format);
			StringBuilder string = processor.process(comparator);
			BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("data.xml")));
			writer.write(string.toString());
			writer.close();
			return new StringBuilder("Write Xml file succeed!");
		}
		
		
//		if(format.equals("line")){
//			dataScanner = new DataScannerImpl(new FileInputStream("data.txt"));
//			List<Customer> list = dataScanner.scan();
//			processor = new ProcessorImpl(list, format);
//			StringBuilder string = processor.process(comparator);
//			return string;
//		}else{
//			dataScanner = new DataScannerImpl(new FileInputStream("data.txt"));
//			List<Customer> list = dataScanner.scan();
//			processor = new ProcessorImpl(list, format);
//			StringBuilder string = processor.process(comparator);
//			BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("data.xml")));
//			writer.write(string.toString());
//			writer.close();
//			return new StringBuilder("Write Xml file succeed!");
//		}
		
	}

}
